package cn.com.jslh.boarpro.util.template;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.util.timedialog.DateChoseDialogUtil;
import cn.com.jslh.boarpro.util.timedialog.DatePickerListener;

/**
 * Created by Administrator on 2017/11/17.
 */

public class DateTempalte extends AddTempBaseItem implements AdapterInterface {
    @Override
    public String getFlag() {
        return "date";
    }

    @Override
    public int getLayout() {
        return R.layout.add_template_date_item_layout;
    }

    @Override
    public SlimInjector<? extends AddTempBaseItem> getSlimInjetor() {
        SlimInjector<DateTempalte> inj = new SlimInjector<DateTempalte>() {
            @Override
            public void onInject(final DateTempalte data, IViewInjector injector) {
                JSONObject config = new JSONObject();
                try {
                    config = new JSONObject(data.getConfig());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                final JSONObject finalConfig = config;
                injector.text(R.id.titleText, data.getTitle())
                        .text(R.id.valueText, data.getPlaceholder())
                        .with(R.id.valueText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                TextView textView =   (TextView)view;
                                try {
                                    String colorStr = finalConfig.getString("valueColor");
                                    if(colorStr.contains("#")&&colorStr.length()==7){
                                        textView.setTextColor(Color.parseColor(colorStr));
                                    }
                                    String valueBgColor = finalConfig.getString("valueBgColor");
                                    if(valueBgColor.contains("#")&&valueBgColor.length()==7){
                                        textView.setBackgroundColor(Color.parseColor(valueBgColor));
                                    }
                                    int valueSize = finalConfig.getInt("valueSize");
                                    if(valueSize!=-1){
                                        textView.setTextSize(valueSize);
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        })
                        .with(R.id.titleText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                TextView textView =   (TextView)view;
                                try {
                                    String colorStr = finalConfig.getString("titleColor");
                                    if(colorStr.contains("#")&&colorStr.length()==7){
                                        textView.setTextColor(Color.parseColor(colorStr));
                                    }
                                    int valueSize = finalConfig.getInt("titleSize");
                                    if(valueSize!=-1){
                                        textView.setTextSize(valueSize);
                                    }
                                    int titleWidth = finalConfig.getInt("titleWidth");
                                    if(titleWidth!=-1){
                                        textView.setWidth(titleWidth);
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        })
                        .clicked(R.id.valueText, new View.OnClickListener() {
                            @Override
                            public void onClick(final View v) {
                                int minDate =-10;
                                int maxDate= 0;
                                try {
                                     minDate = finalConfig.getInt("minDate");
                                     maxDate = finalConfig.getInt("maxDate");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                DateChoseDialogUtil.showTimeDialog(v.getContext(),minDate,maxDate,new DatePickerListener() {
                                    @Override
                                    public void picker(String date) {
                                        Toast.makeText(v.getContext(), date, Toast.LENGTH_SHORT).show();
                                        data.setResult(date);
                                        ((TextView)v).setText(date);
                                    }
                                });
                            }
                        });
            }
        };
        return inj;
    }
}
